<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-8-3
 * Time: 16:58
 */

namespace App\Repositories;


use App\Models\OperationRecord;
use App\Models\Student;
use App\Models\StudentCount;

class StudentCountRepository extends BaseRepository{

    public function __construct(
        StudentCount $studentCount
    )
    {
        $this->model = $studentCount;
    }

    public function index($param, $extra = '', $size = 10)
    {
        $info = $this->model->where('student_id', $param['student_id'])->first();
        $list = OperationRecord::leftJoin('coachs', 'coachs.id', '=', 'yy_operation_record_log.coach_id')
                                ->leftJoin('yy_put_periods', 'yy_put_periods.id', '=', 'yy_operation_record_log.putime_id')
                                ->select('yy_operation_record_log.*','coachs.name as coachname','yy_put_periods.km')->where('student_id', $param['student_id'])
                                ->limit($param['size'])->offset(($param['page'] - 1) * $size)->orderBy('id', 'desc')->get();

        if($info){

            $data['total'] = $info->km2period + $info->km3period;
            $data['km2total'] = $info->km2period;
            $data['km3total'] =  $info->km3period;
            $data['list'] = $list;
            return $data;
        }
        return [];
    }

    /**
     * 添加
     * @param array $inputs
     * @param string $extra
     * @return mixed
     */
    public function store($inputs = [], $extra = '')
    {
        $model = new $this->model;
        return $this->save($model,$inputs);
    }


    /**
     * @param int $id
     * @param string $extra
     * @return int
     */
    public function destroy($id, $extra = '')
    {
        $list = $this->getById($id);
        return $list->delete();
    }

    /**
     * 修改
     * @param int $id
     * @param array $inputs
     * @param string $extra
     * @return mixed
     */
    public function update($id, $inputs, $extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$inputs);
    }

    /**
     * @param $model
     * @param $inputs
     * @return mixed
     */
    public function save($model, $inputs)
    {
        if (array_key_exists('school_id',$inputs)) {
            $model->school_id = e($inputs['school_id']);//驾校id
        }
        if (array_key_exists('student_id',$inputs)) {
            $model->student_id = e($inputs['student_id']);//学员id
        }
        //统计信息
        if (array_key_exists('km2period',$inputs)) {
            $model->km2period = e($inputs['km2period']);//第二部分剩余学时
        }
        if (array_key_exists('km2accomplish',$inputs)) {
            $model->km2accomplish = e($inputs['km2accomplish']);//第二部分已完成学时
        }
        if (array_key_exists('km2sumperiod',$inputs)) {
            $model->km2sumperiod = e($inputs['km2sumperiod']);//第二部分总学时
        }
        if (array_key_exists('km3period',$inputs)) {
            $model->km3period = e($inputs['km3period']);//第三部分剩余学时
        }
        if (array_key_exists('km3accomplish',$inputs)) {
            $model->km3accomplish = e($inputs['km3accomplish']);//第三部分已完成学时
        }
        if (array_key_exists('km3sunperiod',$inputs)) {
            $model->km3sunperiod = e($inputs['km3sunperiod']);//第三部分总学时
        }
        if (array_key_exists('pickupnum',$inputs)) {
            $model->pickupnum = e($inputs['pickupnum']);//接送剩余次数
        }
        if (array_key_exists('pickupaccomplish',$inputs)) {
            $model->pickupaccomplish = e($inputs['pickupaccomplish']);//已接送次数
        }
        if (array_key_exists('pickupsum',$inputs)) {
            $model->pickupsum = e($inputs['pickupsum']);//接送总次数
        }
        $model->save();
        return $model;

    }

    /**
     * 获取学员统计信息
     * @param $student_id
     * @param $school_id
     * @return mixed
     */
    public function getByStudentInfo($student_id, $school_id)
    {
        return $this->model->where('student_id',$student_id)->where('school_id',$school_id)->first();
    }

    /**
     * @param int $id
     * @param array|string $extra
     */
    public function edit($id,$extra)
    {
        // TODO: Implement edit() method.
    }


    /**
     * 学时剩余总计
     */
    public function walletTotal(Student $student)
    {
        $info = $this->model->where('student_id', $student->id)->first();

        if($info){

            return $info->km2period + $info->km3period;
        }
        return 0;
    }


    /**
     * 学员预约信息
     * @param Student $student
     */
    public function getSubInfo(Student $student)
    {

        $info = $this->model->where('student_id', $student->id)->first();

        $data['sub2total'] = $info ? $info->km2accomplish + $info->km2period : 0;
        $data['sub3total'] = $info ? $info->km3accomplish + $info->km3period : 0;
        $data['sub2period'] = $info ? $info->km2accomplish : 0;
        $data['sub3period'] = $info ? $info->km3accomplish : 0;

        return $data;
    }


}